package com.get_all_file_for_folder;

import org.jetbrains.annotations.NotNull;

import java.io.*;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @ClassName: GetAllFileForFolderAndCopyToTargetFolder
 * @Author: ZhangYue26
 * @Description: 获取指定文件夹下的所有某种格式文件，并复制到指定文件夹(单层)
 * @Date: 2020-10-31
 */
public class GetAllFileForFolderAndCopyToTargetFolder {
    public static List<String> retainList = Arrays.asList("png","jpg","mp4","avi");//需要保留的文件类型的后缀名
    public static double requireCapacity = 0;

    public static void main(String[] args) throws Exception {
        List<File> fileList = getFiles(new File("D:\\自学书籍文档"));
        System.out.println("总共"+fileList.size()+"个文件,需要"+Math.ceil(requireCapacity/1024/1024)+"MB "+Math.ceil(requireCapacity/1024/1024/1024)+ "GB空间");
        listTarget(fileList,"F:\\ttttt");


    }

    /**
     * 将文件复制到指定文件夹
     * @param fileList
     * @param targetDirectory
     * @throws Exception
     */
    private static void listTarget(List<File> fileList, String targetDirectory) throws Exception {
        File target = new File(targetDirectory);
        int i = 0;
        if (!target.exists()) {
            target.mkdir();
        }
        OutputStream outputStream = null;
        InputStream inputStream = null;
        for (File file : fileList) {
            byte[] bytes = new byte[1024 * 8];
            inputStream = new FileInputStream(file);
//            outputStream = new FileOutputStream(targetDirectory + "\\" + file.getName());
            outputStream = new FileOutputStream(targetDirectory + "\\" + System.currentTimeMillis()+ UUID.randomUUID().toString().replaceAll("-", "").substring(0,5) + "." + file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length()));
            while (-1 != inputStream.read(bytes)) {
                outputStream.write(bytes);
                outputStream.flush();
                bytes = new byte[1024 * 8];
            }
            outputStream.flush();
            outputStream.close();
            inputStream.close();
            i++;
        }
        System.out.println("完成"+i+"个文件传输");
    }


    /**
     * @Description 获取某一文件夹下的所有文件
     * @author ZhangYue26
     * @date 2020/8/11
     * @param [file]
     * @return java.util.List<java.io.File>
     **/
    @NotNull
    public static List<File> getFiles(File file) {
        List<File> fileList = new ArrayList<>();
        if (file.isFile() && retainList.contains(file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length()).toLowerCase())) {
            requireCapacity += file.length();
            return Collections.singletonList(file);
        } else if (file.isFile() && !retainList.contains(file.getName().substring(file.getName().lastIndexOf(".") + 1, file.getName().length()).toLowerCase())) {
            return Collections.EMPTY_LIST;
        }
        for (File directoryFile : file.listFiles()) {
            fileList.addAll( getFiles(directoryFile) );
        }
        fileList = fileList.stream().filter(val -> val != null).collect(Collectors.toList());
        return fileList;
    }

}
